#include <iostream>
#include <pthread.h>
#include <cstdio>
#include <string>
#include <unistd.h>

using namespace std;

void* handler(void* args)
{
    const string s = (char*)args;
    while(1)
    {
        cout << "name:" << s << "  pid:" << getpid() << endl;
        sleep(1);
    }
}

int main()
{   
    //1.创建pthread_t（存储线程id?）
    pthread_t tid[5];
    char name[64];
    for(int i = 0;i < 5;i++)
    {
        snprintf(name,sizeof(name),"%s-%d","thread",i);
        //2.创建线程（线程id，手动设置新建线程的属性，函数指针的方式代表执行的函数，传入函数指针中的值）
        pthread_create(tid+i,nullptr,handler,(void*)name);
        sleep(1); //缓解传参产生的bug
    }

    //主线程
    while(1)
    {
        cout << "i am main thread,pid:" << getpid() << endl;
        sleep(3);
    }   

    return 0;
}